package cc.catface.parent;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import cc.catface.base.db.DBHelper;
import cc.catface.base.db.table.User;
import cc.catface.base.net.core.BizObserver;
import cc.catface.base.net.RequestCenter;
import cc.catface.base.net.core.RetrofitHelper;
import cc.catface.base.net.domain.ResultBody;
import cc.catface.commom.java.TThreadPool;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btAdd).setOnClickListener(v -> {

           // TThreadPool.EXECUTOR.submit(()->DBHelper.getInstance().getUserDao().insert(new User("zhangsan", 19, System.currentTimeMillis())));
            Observable<List<User>> observable = DBHelper.getInstance().getUserDao().allUsers();
            observable.subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(users -> {
                        Toast.makeText(MainActivity.this, "nini", Toast.LENGTH_SHORT).show();
                        RetrofitHelper.d(Thread.currentThread() + ", users:" + new Gson().toJson(users));
                    });
        });

        findViewById(R.id.btTime).setOnClickListener(v -> {
          TThreadPool.EXECUTOR.submit(()->  DBHelper.getInstance().getUserDao().deleteAll());
//            RequestCenter.time(new BizObserver<String>(MainActivity.this) {
//                @Override
//                public void onSuccess(String data) {
//
//                    super.onSuccess(data);
//                }
//
//                @Override
//                public void onFailure(String code, String message) {
//                    RetrofitHelper.d("====>"+ message);
//                    super.onFailure(code, message);
//                }
//            });

            ResultBody body = new ResultBody();
            body.setCode("00");
            body.setMessage("msg");
            body.setData(111);
            RequestCenter.paramE(body, new BizObserver<ResultBody>() {
                @Override
                public void onSuccess(ResultBody data) {
                    RetrofitHelper.d("--->" + new Gson().toJson(data));
                    super.onSuccess(data);
                }

                @Override
                public void onFailure(String code, String message) {
                    RetrofitHelper.d("====>" + message);
                    super.onFailure(code, message);
                }
            });
        });
    }
}